package net.xdclass.forum.service.impl;

import net.xdclass.forum.domain.Reply;
import net.xdclass.forum.domain.Topic;
import net.xdclass.forum.domain.User;
import net.xdclass.forum.dto.PageDTO;
import net.xdclass.forum.dao.TopicDao;
import net.xdclass.forum.service.TopicService;

import java.time.LocalDateTime;
import java.util.List;

public class TopicServiceImpl implements TopicService {

    TopicDao topicDao = new TopicDao();

    @Override
    public PageDTO<Topic> listTopicPageByCid(int cId, int page, int pageSize) {

        int totalRecordNum = topicDao.countTotalTopicByCid(cId);
        int from = (page - 1) * pageSize;

        //分页查询
        List<Topic>  topicList = topicDao.findListByCid(cId, from, pageSize);

        PageDTO<Topic> pageDTO = new PageDTO<>(page, pageSize, totalRecordNum);
        pageDTO.setList(topicList);

        return pageDTO;
    }

    /*
    查询该topic下面的所有reply
     */
    @Override
    public PageDTO<Reply> findReplyPageByTopicId(int topicId, int page, int pageSize) {
        int totalReplyNum = topicDao.countTotalReplyByTopicId(topicId);

        int from = (page - 1) * pageSize;

        //分页查询
        List<Reply>  replyList = topicDao.findReplyListByTopicId(topicId, from, pageSize);
        PageDTO<Reply> pageDTO = new PageDTO<>(page, pageSize, totalReplyNum);
        pageDTO.setList(replyList);
        return pageDTO;
    }

    /*
    查询该topic的信息
     */

    @Override
    public Topic findById(int topicId) {

        return topicDao.findById(topicId);
    }

    /*
    添加主题
     */
    @Override
    public int addTopic(User loginUser, String title, int c_id, String content) {
        Topic topic = new Topic();


        topic.setC_id(c_id);
        topic.setTitle(title);
        topic.setContent(content);
        topic.setPv(100);
        topic.setUser_id(loginUser.getId());
        topic.setUsername(loginUser.getUsername());
        topic.setUser_img(loginUser.getImg());
        topic.setCreate_time(LocalDateTime.now());
        topic.setUpdate_time(LocalDateTime.now());
        topic.setHot(0);
        topic.setDelete(0);

        int row = 0;
        try {
            row = topicDao.save(topic);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return row;
    }

    @Override
    public int replyByTopicId(User loginUser, int topicId, String content) {
        //获取最新楼层
        int floor = topicDao.findLatestFloorByTopicId(topicId);
        if(floor <0)
        {
            floor = 0;
        }

        //设置回复的参数
        Reply reply = new Reply();
        reply.setTopic_id(topicId);
        reply.setFloor(floor+1);
        reply.setContent(content);
        reply.setUser_id(loginUser.getId());
        reply.setUsername(loginUser.getUsername());
        reply.setUser_img(loginUser.getImg());
        reply.setCreate_time(LocalDateTime.now());
        reply.setUpdate_time(LocalDateTime.now());
        reply.setDelete(0);

        int row = 0;
        try {
            row = topicDao.replyByTopicId(reply);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return row;
    }
}
